<template>
    <h2 style="text-align: center;">添加房号信息</h2>
    <table border="1" class="table table-bordered">
        <tbody>
            <tr>
                <th>房号名称</th>
                <th>
                    <input type="text" placeholder="请输入房号名称" v-model="Info.RoomNum_Name" />
                </th>
            </tr>

            <tr>
                <th>房号状态</th>
                <th>
                    <input type="radio" :value="true" checked   v-model="Info.RoomNum_State"/>上架
                    <input type="radio" :value="false"   v-model="Info.RoomNum_State"/>下架
                </th>
            </tr>

            <tr>
                <th>房型名称</th>
                <th>
                    <select v-model="Info.RoomType_Id">
                        <option :value="-1">请选择</option>
                        <option v-for="item in InfoType" :value="item.RoomType_Id">{{ item.RoomType_Name }}</option>
                    </select>
                </th>
            </tr>

            <tr>
                <th colspan="2" style="text-align: center;">
                    <input type="button" value="添加房号信息" class="btn btn-success" @click="AddRoomNum" style="margin-right: 10px;" />
                    <a href="/ShowRoomNum">返回列表</a>
                </th>
            </tr>
        </tbody>
    </table>
</template>

<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';
    import { useRouter  } from 'vue-router';

    const router=useRouter();
    //添加响应式数据
    const Info=ref({
        RoomNum_Name: "",
        RoomNum_State: true,
        RoomType_Id: -1
    })
    //房型响应式数据
    const InfoType=ref([{
        RoomType_Id: 0,
        RoomType_Name: "",
    }])
    onMounted(()=>{
        GetRoomType();//获取房型信息
    })
    //获取房型信息
    const GetRoomType=()=>{
        axios.get("https://localhost:44386/api/RoomNum/GetRoomType")
        .then(res=>{
            InfoType.value=res.data;
        })
    }
    //添加
    const AddRoomNum=()=>{
        axios.post("https://localhost:44386/api/RoomNum/AddRoomNum",Info.value)
        .then(res=>{
            if(res.data>0)
            {
                alert('房号信息添加成功');
                router.push("/ShowRoomNum");//跳转到显示页面
            }
            else
            {
                alert('房号信息添加失败');
            }
        })
    }
</script>